package com.doitedu.hbase.client;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Table;

import java.io.IOException;

/**
 * @Date 2021/12/13
 * @Created by HANGGE
 * @Description
 * 1 插入数据
 *   1) 插入单元格  addColumn添加单元格
 */
public class C02_Inser {
    public static void main(String[] args) throws Exception {
        // 1 获取连接
        Configuration conf = HBaseConfiguration.create();
        conf.set("hbase.zookeeper.quorum" , "linux01:2181,linux02:2181,linux03:2181");
        Connection conn = ConnectionFactory.createConnection(conf);

         // 2 获取操作的核心对象
        Table table = conn.getTable(TableName.valueOf("tb_teacher"));
        //插入数据  put  'tb_teacher'  ,'行键' , 'cf:属性'  , '值'[二进制]
        /**
         * ut(byte[] row)
         */
        Put put = new Put("rk002".getBytes());
        /**
         * 参数一  列族
         * 参数二  属性
         * 参数三   值
         * 里面的内容都是字节数组
         */
        put.addColumn("cf".getBytes() , "id".getBytes() ,"0002".getBytes());
        put.addColumn("cf".getBytes() , "name".getBytes() ,"ly".getBytes());
        put.addColumn("cf".getBytes() , "age".getBytes() ,"43".getBytes());
        put.addColumn("cf".getBytes() , "city".getBytes() ,"HEB".getBytes());
         // 执行插入
        table.put(put);
        // 3 释放资源
        table.close();
        conn.close();

    }
}
